package leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @description:
 * @author: ywk
 * @date: 2023-11-19
 */
public class P637 {
    public static void main(String[] args) {

    }
    public static List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        List<Double> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            int size = queue.size();
            double total = 0,num=size;
            while (size-- > 0) {
                TreeNode poll = queue.poll();
                total += poll.val;
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }
            result.add(total / num);
        }
        return result;
    }
}
